package com.sk.cardeal.adapter;

import android.graphics.Color;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.cardeal.R;
import com.sk.cardeal.callback.CountCallback;
import com.sk.cardeal.callback.onGroupExpandedListener;
import com.sk.cardeal.entity.NewPartsList;
import java.util.List;
import java.util.Random;


/**
 * @author Richie on 2017.07.31
 *         改过 Indicator 的 ExpandableListView 的适配器
 */
public class IndicatorExpandableListAdapter extends BaseExpandableListAdapter {
    private static final String TAG = "IndicatorExpandableList";
    private List<NewPartsList.ResultListBean> groupData;
    private List<NewPartsList.ResultListBean> childData;
    private CountCallback callback;

    public CountCallback getCallback() {
        return callback;
    }

    public void setCallback(CountCallback callback) {
        this.callback = callback;
    }

    public IndicatorExpandableListAdapter(List<NewPartsList.ResultListBean> groupData, List<NewPartsList.ResultListBean> childData) {
        this.groupData = groupData;
        this.childData = childData;
    }


    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childData.get(groupPosition).getPartsQualityList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childData.get(groupPosition).getPartsQualityList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_group_indicator, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.tv_name = convertView.findViewById(R.id.tv_name);
            groupViewHolder.icon = convertView.findViewById(R.id.icon);
            groupViewHolder.tv_num = convertView.findViewById(R.id.tv_num);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        boolean ischoose = false;
        for(NewPartsList.ResultListBean.PartsQualityListBean bean : groupData.get(groupPosition).getPartsQualityList()){
            if(bean.getWantCount()>0){
                ischoose = true;
                break;
            }
        }

        if(ischoose){
            groupViewHolder.icon.setBackgroundResource(R.mipmap.project_select);
        }else {
            groupViewHolder.icon.setBackgroundResource(R.mipmap.project_unselect);
        }

        groupViewHolder.tv_num.setText(groupData.get(groupPosition).getPartsCount()+"个");
        groupViewHolder.tv_name.setText(groupData.get(groupPosition).getPartsName());

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_expand_child, parent, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.tv_num = convertView.findViewById(R.id.tv_num);
            childViewHolder.add = convertView.findViewById(R.id.add);
            childViewHolder.reduce = convertView.findViewById(R.id.reduce);
            childViewHolder.tv_quality = convertView.findViewById(R.id.tv_quality);
            childViewHolder.tv_stock = convertView.findViewById(R.id.tv_stock);
            childViewHolder.ll = convertView.findViewById(R.id.ll);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        String qs = "";
        switch (groupData.get(groupPosition).getPartsQualityList().get(childPosition).getQuality()){
            case 1:
                qs = "原厂件";
                break;
            case 2:
                qs = "品牌件";
                break;
            case 3:
                qs = "改装件";
                break;
            case 4:
                qs = "自制件";
                break;
            case 5:
                qs = "修复件";
                break;
            case 6:
                qs = "拆车件";
                break;
        }

        childViewHolder.tv_quality.setText(qs);
        childViewHolder.tv_num.setText(String.valueOf(groupData.get(groupPosition).getPartsQualityList().get(childPosition).getWantCount()));

        switch (groupData.get(groupPosition).getPartsQualityList().get(childPosition).getPurchaseState()){
            case 0:
                childViewHolder.tv_stock.setText("未采购过");
                childViewHolder.tv_stock.setTextColor(Color.parseColor("#e5b252"));
                break;
            case 1:
                childViewHolder.tv_stock.setText("库存:"+groupData.get(groupPosition).getPartsQualityList().get(childPosition).getCount());
                childViewHolder.tv_stock.setTextColor(Color.parseColor("#b5bfcd"));
                break;
        }

        childViewHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.count(1,groupPosition,childPosition,groupData.get(groupPosition).getPartsQualityList().get(childPosition));
            }
        });

        childViewHolder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = groupData.get(groupPosition).getPartsQualityList().get(childPosition).getWantCount();
                if(count>0){
                    callback.count(0,groupPosition,childPosition,groupData.get(groupPosition).getPartsQualityList().get(childPosition));
                }
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class GroupViewHolder {
        TextView tv_name;
        ImageView icon;
        TextView tv_num;
    }

    private static class ChildViewHolder {
        TextView tv_quality;
        TextView tv_stock;
        TextView add,reduce,tv_num;
        RelativeLayout ll;
    }
}
